package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Fengkai
 * 给定一个字符串 只包含字母和数字
 * 按要求找出字符串中的最长连续子串的长度
 * 字符串本身是其最长的子串
 * 子串要求
 * 只包含一个字母(a~z A~Z)其余必须是数字
 * 字母可以在子串中的任意位置
 * 如果找不到满足要求的子串 比如说，全是字母或数字则返回-1
 * @creat 2023/4/4
 */
public class FindLogesetStr {
    public static void main (String[] args) {
        //对数器
        for (int i = 0; i < 300; i++) {
            String str = getString();
            if (!(getMyStr(str) == hisMethod(str))) {
                System.out.println("error!!!");
            }
        }

    }

    //随机一串长度65535的含有大小写字母+数字的字符串
    public static String getString () {
        int size = (int) ((Math.random() * 65535) + 1);
        Random random = new Random();
        char[] chars = new char[size];
        for (int i = 0; i < size; i++) {
            int caseInt = (int) (Math.random() * 3 + 1);
            switch (caseInt){
                case 1 :
                    chars[i] = (char)(int)(Math.random() * 9 + 1);
                    break;
                case 2 :
                    chars[i] = (char)(int)(random.nextInt(26) + 97);
                    break;
                case 3 :
                    chars[i] = (char)(int)(random.nextInt(26) + 65);
                    break;
            }
        }
        return String.valueOf(chars);
    }

    public static Integer getMyStr (String str) {
//        Pattern compile = Pattern.compile("[^a-zA-Z][0-9]*");
//        StringBuffer stringBuffer = new StringBuffer();
//        Matcher matcher = compile.matcher(str);
//        while (matcher.find()) {
//            System.out.println(matcher.group());
//            matcher.appendReplacement(stringBuffer, matcher.group().replaceAll("[(0-9)]", ""));
//        }
        String s = str.replaceAll("[0-9]", "");
        if (s.length() == 0 || s.length() == str.length()) {
            return -1;
        }
        System.out.println(str);

        //滑动窗口解
        int maxSize = 0;
        int left = 0, right = 0, count = 0;

        Pattern compile = Pattern.compile("[a-zA-Z]");

        out:
        while (left <= str.length() - 1) {
            right = left;
            for ( ;right < str.length(); right++) {
                if (compile.matcher(String.valueOf(str.charAt(right))).find()) {
                    count++;
                }
                if (count == 2) {
                    maxSize = Math.max(maxSize, right - left);
                    left ++;
                    count = 0;
                    continue out;
                }else {
                    maxSize = Math.max(maxSize, right - left + 1);
                }
            }
            left++;
        }
        return maxSize;
    }


    public static int hisMethod(String inputStr) {
        try {
            if (inputStr == null || inputStr.length() == 0) {
                System.out.println("-1");
                return -1;
            }
            char[] chars = inputStr.toCharArray();
            for (char c : chars) {
                if (!(('0' <= c && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {
                    return -1;
                }
            }
            int leftIndex = 0;
            int rightIndex = 1;
            int max = -1;
            while (rightIndex < inputStr.length() && leftIndex < inputStr.length()) {
                ++rightIndex;
                String subStr = inputStr.substring(leftIndex, rightIndex);
                boolean flag;
                String replaceStr = subStr.replaceAll("[0-9]", "");
                flag = replaceStr.length() != subStr.length() && replaceStr.length() <= 1;
                if (flag) {
                    max = Math.max(max, subStr.length());
                } else {
                    leftIndex++;
                }
            }
          return max;

        } catch (Exception e) {
            e.printStackTrace();
           return -1;
        }
    }
}
